package org.springboot.flowable.controller;

import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springboot.flowable.response.ResultBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * </p>
 *
 * @author zsk
 * @date 2020/10/17 下午4:18
 */
@RestController
@RequestMapping("/holidayRequest")
@Slf4j
public class HolidayRequestController {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;
    /**
     * 部署
     */
    @PostMapping("/deploy")
    @ResponseBody
    public ResultBody deploy(){
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("holiday-request.bpmn20.xml")
                .deploy();
        return ResultBody.success(deployment.getId());
    }

    /**
     * 根据流程定义id查询流程定义
     * @param id 流程定义id
     * @return
     */
    @PostMapping("/processDefinitionQuery")
    @ResponseBody
    public ResultBody processDefinitionQuery(String id){
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(id)
                .singleResult();
        log.debug("Found process definition : {}",processDefinition.getName());
        return ResultBody.success(processDefinition);
    }

    /**
     * 启动流程实例
     * @param processInstanceKey 流程实例key
     * @param employee
     * @param nrOfHolidays
     * @param description
     * @return
     */
    @PostMapping("/execution")
    @ResponseBody
    public ResultBody execution(String processInstanceKey,String employee,String nrOfHolidays,String description){
        Map<String, Object> variables = new HashMap<>();
        variables.put("employee", employee);
        variables.put("nrOfHolidays", nrOfHolidays);
        variables.put("description", description);
        ProcessInstance processInstance =
                runtimeService.startProcessInstanceByKey(processInstanceKey, variables);
        return ResultBody.success(processInstance);
    }

    /**
     * 查询候选用户组的任务
     * @param candidateGroup 候选用户组
     * @return
     */
    @PostMapping("/taskQuery")
    @ResponseBody
    public ResultBody taskQuery(String candidateGroup){
        List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup(candidateGroup).list();
        return ResultBody.success(tasks);
    }

    /**
     * 根据任务id显示任务
     * @param taskId 任务id
     * @return
     */
    @PostMapping("/showTaskById")
    @ResponseBody
    public ResultBody showTaskById(String taskId){
        Map<String, Object> processVariables = taskService.getVariables(taskId);
        String showTask=processVariables.get("employee") + " wants " +
                processVariables.get("nrOfHolidays") + " of holidays. Do you approve this?";
        return ResultBody.success(showTask);
    }

    /**
     * 根据任务id显完成任务
     * @param taskId
     * @param approved
     * @return
     */
    @PostMapping("/completeTaskById")
    @ResponseBody
    public ResultBody completeTaskById(String taskId,boolean approved){
        Map<String,Object> variables = new HashMap<>();
        variables.put("approved", approved);
        taskService.complete(taskId, variables);
        return ResultBody.success();
    }
}
